<template>
  <div class="product-list-page">
    <!-- 筛选条件 -->
    <div class="filters">
      <div class="filter-group">
        <span>价格区间：</span>
        <input type="number" v-model="priceRange.min" placeholder="最低">
        <span>-</span>
        <input type="number" v-model="priceRange.max" placeholder="最高">
      </div>
      <div class="filter-group">
        <span>品牌：</span>
        <select v-model="selectedBrand">
          <option value="">全部</option>
          <option v-for="brand in brands" :key="brand" :value="brand">{{ brand }}</option>
        </select>
      </div>
      <div class="filter-group">
        <span>排序：</span>
        <select v-model="sortBy">
          <option value="price">价格</option>
          <option value="sales">销量</option>
          <option value="rating">评分</option>
        </select>
      </div>
    </div>
    <!-- 商品列表 -->
    <div class="products">
      <div v-for="product in filteredProducts" :key="product.id" class="product-card">
        <img :src="product.image" alt="商品图片" class="product-img">
        <h3 class="product-name">{{ product.name }}</h3>
        <p class="product-price">￥{{ product.price }}</p>
        <p class="product-sales">销量：{{ product.sales }}</p>
        <p class="product-rating">评分：{{ product.rating }}★</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

// 模拟商品数据 - 使用网上示例图片
const products = ref([
  { id: 1, name: '商品1', price: 199, sales: 100, rating: 4.5, image: 'https://picsum.photos/id/1/250/200', brand: '品牌A' },
  { id: 2, name: '商品2', price: 299, sales: 200, rating: 4.2, image: 'https://picsum.photos/id/2/250/200', brand: '品牌B' },
  { id: 3, name: '商品3', price: 399, sales: 150, rating: 4.8, image: 'https://picsum.photos/id/3/250/200', brand: '品牌A' }
])

// 筛选条件
const priceRange = ref({ min: null, max: null })
const selectedBrand = ref('')
const sortBy = ref('price')

// 品牌列表
const brands = computed(() => [...new Set(products.value.map(p => p.brand))])

// 过滤排序后的商品
const filteredProducts = computed(() => {
  let filtered = products.value.filter(p => {
    const priceCheck = (priceRange.value.min ? p.price >= priceRange.value.min : true) && (priceRange.value.max ? p.price <= priceRange.value.max : true)
    const brandCheck = selectedBrand.value ? p.brand === selectedBrand.value : true
    return priceCheck && brandCheck
  })

  return filtered.sort((a, b) => {
    if (sortBy.value === 'price') return a.price - b.price
    if (sortBy.value === 'sales') return b.sales - a.sales
    if (sortBy.value === 'rating') return b.rating - a.rating
  })
})
</script>

<style scoped>
.product-list-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.filters {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
  flex-wrap: wrap;
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.filter-group input {
  width: 80px;
  padding: 5px;
}

.filter-group select {
  padding: 5px;
  border-radius: 4px;
}

.products {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}

.product-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 10px;
  text-align: center;
}

.product-img {
  width: 100%;
  height: 200px;
  object-fit: cover;
  margin-bottom: 10px;
}

.product-name {
  font-size: 16px;
  margin: 0 0 8px 0;
}

.product-price {
  font-size: 18px;
  color: #e4393c;
  margin: 0 0 5px 0;
}

.product-sales, .product-rating {
  font-size: 14px;
  color: #666;
  margin: 0 0 5px 0;
}
</style>
    